package com.example.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import com.example.common.Result;
import com.example.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件接口
 */
@Slf4j
@RestController
@RequestMapping("/files")
public class FileController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        log.info("文件上传，文件名:{}", file.getOriginalFilename());
        String url = aliOSSUtils.upload(file);
        log.info("文件上传成功，url:{}",url);
        return Result.success(url);
    }
    @PostMapping("/wang/upload")
    public Map<String, Object> wangEditorUpload(MultipartFile file) throws IOException {

        log.info("富文本图片文件上传，文件名:{}", file.getOriginalFilename());
        String url = aliOSSUtils.upload(file);
        log.info("富文本图片文件上传成功，url:{}",url);
        Map<String, Object> resMap = new HashMap<>();
        //wangEditor上传图片成功后， 需要返回的参数
        resMap.put("errno", 0);
        resMap.put("data", CollUtil.newArrayList(Dict.create().set("url", url)));
        return resMap;
    }
}
